﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ChineseChess.Game.Rules
{
    class MaRule : Rule
    {
        private List<Piece> pies = null;
        public override bool IsDown { get; set; }
        public override List<Point> getNextPos(Piece pie, List<Piece> pies)
        {
            this.pies = pies;
            List<Point> points = new List<Point>();

            int x = pie.X + 2;
            int y = pie.Y + 1;
            this.Add(points, pie, x, y);

            x = pie.X + 2;
            y = pie.Y - 1;
            this.Add(points, pie, x, y);

            x = pie.X - 2;
            y = pie.Y + 1;
            this.Add(points, pie, x, y);

            x = pie.X - 2;
            y = pie.Y - 1;
            this.Add(points, pie, x, y);

            x = pie.X + 1;
            y = pie.Y + 2;
            this.Add(points, pie, x, y);

            x = pie.X + 1;
            y = pie.Y - 2;
            this.Add(points, pie, x, y);

            x = pie.X - 1;
            y = pie.Y + 2;
            this.Add(points, pie, x, y);

            x = pie.X - 1;
            y = pie.Y - 2;
            this.Add(points, pie, x, y);

            return points;
        }

        private void Add(List<Point> points,Piece pie,int x,int y)
        {
            if(isRight(pie,x,y))
            {
                points.Add(new Point(x, y));
            }
        }

        private bool isRight(Piece pie, int x, int y)
        {
            if (x >= 9 || x < 0 || y < 0 || y >= 10)
            {
                return false;
            }

            int x1 = 0;
            int y1 = 0;
            if (Math.Abs(pie.X - x) == 2)
            {
                x1 = pie.X > x ? x + 1 : x - 1;
                y1 = pie.Y;
            }
            else
            {
                x1 = pie.X;
                y1 = pie.Y > y ? y + 1 : y - 1;
            }
             
            if (Chessboard.getPieByXy(x1, y1, pies) != null) //蹩马腿
            {
                return false;
            }

            Piece p = Chessboard.getPieByXy(x, y, pies);
            if (p != null && p.isRed == pie.isRed) //同一方的子
            {
                return false;
            }

            return true;
        }
    }
}
